package net.j4love.zookeeper.quickstart;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author He Peng
 * @create 2017-05-15 14:35
 * @update 2017-05-15 14:35
 * @see
 */
public class UpdateZNode {

    private static final Logger LOG = LoggerFactory.getLogger(UpdateZNode.class);

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {

        ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", 1000, null);
        zk.create("/update_test", "小明和李华是好朋友".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);

        byte[] dataByte = zk.getData("/update_test", null, null);
        String data = new String(dataByte);

        LOG.debug("修改前的 /update_test 节点数据 {}" , data);

        Stat stat = zk.setData("/update_test", "hadoop的吉祥物是创始人 Doug Cutting 孩子的大象玩具".getBytes(), -1);
        data = new String(zk.getData("/update_test", null, null));

        LOG.debug("修改后的 /update_test 节点数据 {}" , data);

        zk.close();
    }
}
